from rest_framework.viewsets import ModelViewSet
from goods.models import SKU,GoodsCategory,SPU,SPUSpecification
from . import sku_serializers
from meiduo_admin.my_paginate import MyPageNumberPagination
from rest_framework.generics import ListAPIView




# 8.获取商品sku信息
class SKUView(ModelViewSet):
    serializer_class = sku_serializers.SKUSerializer
    pagination_class = MyPageNumberPagination
    # queryset = SKU.objects.all()

    # 1.重写数据源方法,过滤数据
    def get_queryset(self):
        # 1.过滤关键字
        keyword = self.request.query_params.get("keyword")

        # 2.判断是否有关键字
        if keyword:
            return SKU.objects.filter(name__contains=keyword)
        else:
            return SKU.objects.all()

# 9.获取商品sku分类(三级分类)
class SKUCategoryView(ListAPIView):
    serializer_class = sku_serializers.SKUCategorySerializer
    queryset = GoodsCategory.objects.filter(subs=None)

# 10.获取商品spu信息
class GoodSimpleView(ListAPIView):
    serializer_class = sku_serializers.GoodSimpleSerializer
    queryset = SPU.objects.all()

# 11.获取sku的商品规格信息
class SKUGoodSpecs(ListAPIView):
    serializer_class = sku_serializers.SKUGoodSpecsSerializer
    # queryset =  SPUSpecification.objects.all()

    def get_queryset(self):
        # 1.获取参数
        spu_id = self.kwargs.get("spu_id")

        # 2.查询规格
        return SPUSpecification.objects.filter(spu_id=spu_id).all()


